package com.ziyueyuwen.xiniao.controller.student;

import com.suyeer.basic.util.HttpResUtil;
import com.suyeer.cache.RedisUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.vo.ZyUserVO;
import com.ziyueyuwen.xiniao.enums.StudentQueryEnum;
import com.ziyueyuwen.xiniao.service.ThirdPartService;
import com.ziyueyuwen.xiniao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;
import static com.ziyueyuwen.xiniao.util.ConstUtil.DATABASE_FOR_UPDATE_USER;
import static com.ziyueyuwen.xiniao.util.ConstUtil.DEFAULT_CACHE_SECOND;
import static com.ziyueyuwen.xiniao.util.ConstUtil.KEY_UPDATE_USER_INFO;

/**
 * @author wancheng  on 2018/12/17.
 */
@Validated
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    UserService userService;
    @Autowired
    ThirdPartService thirdPartService;

    @RequestMapping(value = "/save_student.do")
    public void saveStudent(HttpServletRequest request,
                            HttpServletResponse response,
                            String studentAccount,
                            String studentName) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("添加学生");
        try {
            ZyUser student = new ZyUser();
            student.setUserAccount(studentAccount);
            student.setUserName(studentName);
            student.setCreateTime(new Date());
            student.setIfSysUser(false);
            student.setIfDelete(false);
            userService.saveStudent(student);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_student_name.do")
    public void updateStudent(HttpServletRequest request,
                              HttpServletResponse response,
                              Integer studentId,
                              String studentName) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改学生姓名");
        try {
            ZyUser student = new ZyUser();
            student.setId(studentId);
            student.setUserName(studentName);
            userService.updateStudentName(student);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_all_student.do")
    public void getAllStudent(HttpServletRequest request,
                              HttpServletResponse response,
                              Integer page,
                              Integer rows,
                              Boolean ifBindWx,
                              StudentQueryEnum queryType,
                              String queryValue) {
        HttpResContent<PageData<ZyUserVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取学生列表数据");
        try {
            PageData<ZyUserVO> pageData = userService.getAllStudent(queryType,queryValue,ifBindWx, page, rows);
            saveSuccessMsgAndData(hrc, pageData);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/remove_student_wx_bind.do")
    public void removeStudentWxBind(HttpServletRequest request,
                                    HttpServletResponse response,
                                    String studentAccount) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("解除学生微信绑定");
        try {
            thirdPartService.removeWxBind(studentAccount);
            RedisUtil.set(DATABASE_FOR_UPDATE_USER, KEY_UPDATE_USER_INFO + studentAccount, studentAccount, DEFAULT_CACHE_SECOND);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
